import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getwidget/getwidget.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/model/product.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

///
/// 分类新增或者编辑
///
class AddEdit extends StatefulWidget {
  const AddEdit({Key? key}) : super(key: key);

  @override
  State<AddEdit> createState() => _AddEditState();
}

class _AddEditState extends State<AddEdit> {
  late ProductClassifyModel item;

  @override
  void initState() {
    super.initState();
    item = ProductClassifyModel(name: '', describe: '');
    dynamic params = Get.arguments;
    if (params != null) {
      item = params as ProductClassifyModel;
    }
  }

  double get leftWidth {
    return Sizes.default80;
  }

  double get rowHeight {
    return Sizes.default60;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar: ToolsUtils.getDefaultBrnAppBar(item.id > 0 ? '编辑分类' : '新增分类'),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              decoration: const BoxDecoration(color: Colors.white),
              padding: const EdgeInsets.symmetric(horizontal: Sizes.default15),
              child: ListBody(
                children: [
                  Container(
                    height: rowHeight,
                    padding: const EdgeInsets.symmetric(vertical: Sizes.default10),
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(children: [
                              TextSpan(
                                  text: '中文名称',
                                  style: StylesUtils.customTextStyle(
                                      fontSize: Sizes.default15, color: ColorsUtils.color333)),
                              TextSpan(
                                  text: '*',
                                  style: StylesUtils.customTextStyle(
                                      fontSize: Sizes.default15, color: ColorsUtils.colorFf0))
                            ]))),
                        Expanded(
                            child: Container(
                          padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          decoration: BoxDecoration(
                              color: ColorsUtils.rgba(247, 247, 247, 1),
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(Sizes.default10))),
                          child: TextField(
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color363, fontSize: Sizes.default16),
                            decoration: InputDecoration(
                                border: const OutlineInputBorder(
                                  borderSide: BorderSide.none,
                                ),
                                isCollapsed: true,
                                filled: true,
                                fillColor: Colors.transparent,
                                contentPadding: const EdgeInsets.symmetric(
                                    horizontal: Sizes.default10, vertical: Sizes.default10),
                                constraints: const BoxConstraints(minHeight: Sizes.default40),
                                hintText: '如招牌菜',
                                hintStyle: StylesUtils.customTextStyle(
                                    fontSize: Sizes.default15, color: ColorsUtils.color999)),
                            keyboardType: TextInputType.text,
                            onChanged: (String content) {},
                            controller: ToolsUtils.editingController(item.name),
                          ),
                        ))
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    padding: const EdgeInsets.symmetric(vertical: Sizes.default10),
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(children: [
                              TextSpan(
                                  text: '分类描述',
                                  style: StylesUtils.customTextStyle(
                                      fontSize: Sizes.default15, color: ColorsUtils.color333)),
                            ]))),
                        Expanded(
                            child: Container(
                          padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          decoration: BoxDecoration(
                              color: ColorsUtils.rgba(247, 247, 247, 1),
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(Sizes.default10))),
                          child: TextField(
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color363, fontSize: Sizes.default16),
                            decoration: InputDecoration(
                                border: const OutlineInputBorder(
                                  borderSide: BorderSide.none,
                                ),
                                isCollapsed: true,
                                filled: true,
                                fillColor: Colors.transparent,
                                contentPadding: const EdgeInsets.symmetric(
                                    horizontal: Sizes.default10, vertical: Sizes.default10),
                                constraints: const BoxConstraints(minHeight: Sizes.default40),
                                hintText: '请输入描述',
                                hintStyle: StylesUtils.customTextStyle(
                                    fontSize: Sizes.default15, color: ColorsUtils.color999)),
                            keyboardType: TextInputType.text,
                            controller: ToolsUtils.editingController(item.describe),
                            onChanged: (String content) {},
                          ),
                        ))
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    padding: const EdgeInsets.symmetric(vertical: Sizes.default10),
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(children: [
                              TextSpan(
                                  text: '是否可用',
                                  style: StylesUtils.customTextStyle(
                                      fontSize: Sizes.default15, color: ColorsUtils.color333)),
                            ]))),
                        Expanded(
                            child: Container(
                          padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          alignment: Alignment.centerRight,
                          child: GFToggle(
                            onChanged: (bool? val) {
                              setState(() {
                                item.available = val!;
                              });
                            },
                            value: item.available,
                            type: GFToggleType.ios,
                            boxShape: BoxShape.circle,
                            enabledTrackColor: ColorsUtils.rgba(255, 110, 83, 1),
                            enabledThumbColor: Colors.white,
                          ),
                        ))
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              height: Sizes.default40,
              margin: const EdgeInsets.only(top: Sizes.default30),
              padding: const EdgeInsets.symmetric(horizontal: Sizes.default15),
              child: FractionallySizedBox(
                widthFactor: 1.0,
                heightFactor: 1.0,
                child: GFButton(
                    onPressed: () {},
                    text: '保存',
                    textStyle: StylesUtils.customTextStyle(fontSize: Sizes.default16),
                    shape: GFButtonShape.pills,
                    type: GFButtonType.solid,
                    size: GFSize.LARGE,
                    color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf6)),
              ),
            ),
            if (item.id > 0)
              Container(
                margin: const EdgeInsets.only(top: Sizes.default20),
                alignment: Alignment.center,
                child: TextButton(
                  onPressed: () {
                    Get.back();
                  },
                  child: Text(
                    '删除',
                    style: StylesUtils.customTextStyle(
                        color: ColorsUtils.colorFf0, fontSize: Sizes.default15),
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}
